package com.dyzx.hbb.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.modules.sys.entity.SysRoleMenu;
import com.dyzx.hbb.modules.sys.service.SysRoleMenuService;
import com.dyzx.hbb.modules.sys.mapper.SysRoleMenuMapper;
import com.dyzx.hbb.modules.sys.service.SysRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【sys_role_menu(创建角色分配菜单表)】的数据库操作Service实现
* @createDate 2025-04-14 13:30:05
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService{
    @Autowired
    private SysRoleUserService roleUserService;
 //menu:miniList 清理缓存删掉
    @CacheEvict(value = "menu:miniList")
    @Override
    public boolean assignMenus(Long roleId, List<Long> menuIds) {
        if (menuIds != null && !menuIds.isEmpty()) {
            if (roleId == null) {
                return false;
            }
        // 先删除原有的角色菜单关联
        deleteRoleMenuAll(roleId);

        // 批量保存新的角色菜单关联

            List<SysRoleMenu> roleMenus = menuIds.stream()
                .map(menuId -> {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(menuId);
                    return roleMenu;
                })
                .toList();
            return this.saveBatch(roleMenus);
        }
        return true;
    }

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        return baseMapper.selectList(
            new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
        ).stream()
        .map(SysRoleMenu::getMenuId)
        .toList();
    }

    @Override
    public List<Long> getMenuIdsByRoleIds(List<Long> roleIds) {
        return baseMapper.selectList(
                        new LambdaQueryWrapper<SysRoleMenu>()
                                .in(SysRoleMenu::getRoleId, roleIds)
                ).stream()
                .map(SysRoleMenu::getMenuId)
                .toList();
    }

    @Override
    public List<Long> getMenuIdsByUserId(Long userId) {
        List<Long> roleIds = roleUserService.getRoleIdsByUserId(userId);
        return getMenuIdsByRoleIds(roleIds);
    }
    @CacheEvict(value = "menu:miniList")
    @Override
    public boolean deleteRoleMenus(Long roleId, List<Long> menuIds) {
        if (roleId == null || menuIds == null || menuIds.isEmpty()) {
            return false;
        }
        return remove(
            new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
                .in(SysRoleMenu::getMenuId, menuIds)
        );
    }
    @CacheEvict(value = "menu:miniList")
    //  @Override
    public boolean deleteRoleMenuAll(Long roleId) {
        return remove(
            new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
        );
    }
}




